package com.yxkj.syh.app.huarong.activities.creat;

import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.bindingAdp.view_click.ClickEvent;
import com.syh.app.basic.utils.Tooast;
import com.yxkj.syh.app.huarong.bean.Linkman;
import com.yxkj.syh.app.huarong.bean.PaymentsResponse;
import com.yxkj.syh.app.huarong.data_center.model.PaymentModel;
import com.yxkj.syh.app.huarong.util.UserManager;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yxkj.syh.app.huarong.adps.AddedGoodsAdp;
import com.yxkj.syh.app.huarong.bean.CreatOrderResponse;
import com.yxkj.syh.app.huarong.bean.Goods;
import com.yxkj.syh.app.huarong.bean.OrderInfo;
import com.yxkj.syh.app.huarong.bean.PaperFactory;
import com.yxkj.syh.app.huarong.bean.Payment;
import com.yxkj.syh.app.huarong.bean.UserMainInfo;
import com.yxkj.syh.app.huarong.bean.UserMainInfoResponse;
import com.yxkj.syh.app.huarong.data_center.model.AccountModel;
import com.yxkj.syh.app.huarong.data_center.model.OrderModel;

import java.util.List;

/**
 * 创建者：SYH
 * 时间：2020/5/21 14:39
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class NewOrderVM extends BaseViewModel<ActivityEvent> {
    private Payment payment;
    private Linkman linkman;
    private PaperFactory paperFactory;
    private AddedGoodsAdp mAddedGoodsAdp;

    public NewOrderVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<String> ofBtnPayment = new ObservableField<>();
    public ObservableField<String> ofBtnFromAddress = new ObservableField<>();
    public ObservableField<String> ofSupplier = new ObservableField<>();
    public ObservableField<String> ofFactory = new ObservableField<>();
    public ObservableField<String> ofPayment = new ObservableField<>();
    public ObservableField<String> ofFromName = new ObservableField<>();
    public ObservableField<String> ofFromPhone = new ObservableField<>();
    public ObservableField<String> ofFromAddress = new ObservableField<>();

    public MutableLiveData<OrderInfo> mldCreatedSuccess = new MutableLiveData<>();

    public ClickEvent nextClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            if (checkParameter()) {
                creatOrderInServer();
            }
        }
    };

    private boolean checkParameter() {
        if (payment != null) {
            if (linkman != null) {
                if (paperFactory != null) {
                    if (mAddedGoodsAdp != null && mAddedGoodsAdp.getData().size() > 0) {
                        return true;
                    } else {
                        Tooast.normalInfo("请添加货品");
                    }
                } else {
                    Tooast.normalInfo("请选择纸厂");
                }
            } else {
                Tooast.normalInfo("请选择供应商");
            }
        } else {
            Tooast.normalInfo("请完成收款信息");
        }
        return false;
    }

    private void creatOrderInServer() {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setDestinationId(paperFactory.getId());
        orderInfo.setBankId(Long.decode(payment.getId()));
        orderInfo.setSalesmanId(UserManager.getUserManager().getUser().getId());
        orderInfo.setSupplierId(linkman.getId());
        List<Goods> goodsList = mAddedGoodsAdp.getData();
        orderInfo.setDetails(goodsList);

        showLoading("creatOrder");
        OrderModel.getOrderModel().creatOrder(orderInfo.getCreatRequest(), mLifecycleProvider.bindToLifecycle(), new ApiObserver<CreatOrderResponse>() {
            @Override
            public void onSuccess(CreatOrderResponse creatOrderResponse) {
                hideLoading("creatOrder");
                OrderInfo orderInfo1 = creatOrderResponse.getData();
                if (orderInfo1.getPayment() == null) {
                    orderInfo1.setPayment(payment);
                }
                if (orderInfo1.getDestination() == null) {
                    orderInfo1.setDestination(paperFactory.getName());
                }
                mldCreatedSuccess.setValue(orderInfo1);
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("creatOrder");
                Tooast.warning(msg);
            }
        });
    }

    private void setPaymentText() {
        ofBtnPayment.set("修改");
        StringBuffer buffer = new StringBuffer();
        buffer.append("姓名：").append(payment.getAccountName()).append("\n");
        switch (payment.getBankName()) {
            case "网银":
                buffer.append("银行卡号：").append(payment.getCardNumber()).append("\n");
                buffer.append("开户行：").append(payment.getOpeningBank());
                break;
            case "支付宝":
                buffer.append("支付宝账户：").append(payment.getCardNumber());
                break;
            case "微信":
                buffer.append("微信账户：").append(payment.getCardNumber());
                break;
        }
        ofPayment.set(buffer.toString());
    }

    /**
     * 设置供应商
     * @param linkman
     */
    public void setLinkman(Linkman linkman) {
        this.linkman = linkman;
        ofSupplier.set(linkman.getName());

        ofBtnFromAddress.set("修改");

        ofFromName.set(linkman.getLinkman());
        ofFromPhone.set(linkman.getMobile());
        ofFromAddress.set("发货地址：" + linkman.getArea() + linkman.getAddress());

        getPaymentList();
    }

    public void setPaperFactory(PaperFactory paperFactory) {
        this.paperFactory = paperFactory;
        ofFactory.set(paperFactory.getName());
    }

    /**
     * 收款列表
     */
    public void getPaymentList() {
        showLoading("paymentMethodList");

        PaymentModel.getPaymentModel().paymentMethodList(linkman.getId(),mLifecycleProvider.bindToLifecycle(), new ApiObserver<PaymentsResponse>() {
            @Override
            public void onSuccess(PaymentsResponse paymentsResponse) {
                hideLoading("paymentMethodList");
                if (paymentsResponse.getData() != null && paymentsResponse
                        .getData().size() > 0) {
                    payment = paymentsResponse.getData().get(0);
                    setPaymentText();
                } else {
                    ofBtnPayment.set("去添加");
                    ofPayment.set("");
                }
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
            }
        });

    }

    public Payment getPayment() {
        return payment;
    }

    public void setPayment(Payment payment) {
        this.payment = payment;
        setPaymentText();
    }


    public Linkman getLinkman() {
        return linkman;
    }

    public void setAddedGoodsAdp(AddedGoodsAdp addedGoodsAdp) {
        this.mAddedGoodsAdp = addedGoodsAdp;
    }
}
